/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.emergency.resources.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.emergency.common.constant.PlanDicConstant;
import com.hopes.bizz.emergency.resources.dto.ErEmergencyResourcesObjDTO;
import com.hopes.bizz.emergency.resources.entity.ErTeamEntity;
import com.hopes.bizz.emergency.resources.mapper.ErTeamMapper;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.bizz.emergency.resources.entity.ErExpertEntity;
import com.hopes.bizz.emergency.resources.service.ErExpertService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 应急专家
 *
 * @author cx
 * @date 2023-11-02 17:47:52
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/resources/expert" )
@Tag(description = "expert" , name = "应急专家管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ErExpertController {

    private final  ErExpertService erExpertService;
    private final ErTeamMapper erTeamMapper;

    /**
     * 分页查询
     * @param page 分页对象
     * @param erExpert 应急专家
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('resources_expert_view')" )
    public R getErExpertPage(Page page, ErExpertEntity erExpert) {
        LambdaQueryWrapper<ErExpertEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StrUtil.isNotBlank(erExpert.getName()),ErExpertEntity::getName,erExpert.getName());
		wrapper.like(StrUtil.isNotBlank(erExpert.getWorkCompany()),ErExpertEntity::getWorkCompany,erExpert.getWorkCompany());
		wrapper.eq(Objects.nonNull(erExpert.getExpertsType()),ErExpertEntity::getExpertsType,erExpert.getExpertsType());
		wrapper.eq(Objects.nonNull(erExpert.getExpertLevel()),ErExpertEntity::getExpertLevel,erExpert.getExpertLevel());
        if(StrUtil.isNotBlank(erExpert.getTeamName())){
            LambdaQueryWrapper<ErTeamEntity> teamQueryWrapper = Wrappers.<ErTeamEntity>lambdaQuery().like(ErTeamEntity::getTeamName, erExpert.getTeamName()).select(ErTeamEntity::getId);
            List<Long> teamList = erTeamMapper.selectObjs(teamQueryWrapper).stream().map(o -> (Long) o).collect(Collectors.toList());
            List<String> teamIdList = teamList.stream().map(String::valueOf).collect(Collectors.toList());
            wrapper.in(!teamIdList.isEmpty(),ErExpertEntity::getTeamId,teamIdList);
        }
        return R.ok(erExpertService.page(page, wrapper));
    }

    /**
     * 查询List
     * @param erExpert 应急专家
     * @return
     */
    @Operation(summary = "查询List" , description = "查询List" )
    @GetMapping("/list" )
    @PreAuthorize("@pms.hasPermission('resources_expert_view')" )
    public R getErExpertList(ErExpertEntity erExpert) {
        LambdaQueryWrapper<ErExpertEntity> queryWrapper = Wrappers.<ErExpertEntity>lambdaQuery().eq(ErExpertEntity::getUseFlag, PlanDicConstant.USE_FLAG_YES);
        return R.ok(erExpertService.list(queryWrapper));
    }


    /**
     * 通过id查询应急专家
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('resources_expert_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(erExpertService.getById(id));
    }


    /**
     * 通过teamId查询专家List
     *
     * @param resourcesObjDTO initId
     * @return R
     */
    @Operation(summary = "通过teamId查询", description = "通过teamId查询")
    @GetMapping("/getListByTeamId")
    public R getListByUnitId(Page page, ErEmergencyResourcesObjDTO resourcesObjDTO) {
        return erExpertService.getListByTeamId(page,resourcesObjDTO);
    }

    /**
     * 新增应急专家
     * @param erExpert 应急专家
     * @return R
     */
    @Operation(summary = "新增应急专家" , description = "新增应急专家" )
    @SysLog("新增应急专家" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('resources_expert_add')" )
    public R save(@RequestBody ErExpertEntity erExpert) {
        return R.ok(erExpertService.save(erExpert));
    }

    /**
     * 修改应急专家
     * @param erExpert 应急专家
     * @return R
     */
    @Operation(summary = "修改应急专家" , description = "修改应急专家" )
    @SysLog("修改应急专家" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('resources_expert_edit')" )
    public R updateById(@RequestBody ErExpertEntity erExpert) {
        return R.ok(erExpertService.updateById(erExpert));
    }

    /**
     * 通过id删除应急专家
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除应急专家" , description = "通过id删除应急专家" )
    @SysLog("通过id删除应急专家" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('resources_expert_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(erExpertService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param erExpert 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('resources_expert_export')" )
    public List<ErExpertEntity> export(ErExpertEntity erExpert) {
        return erExpertService.list(Wrappers.query(erExpert));
    }
}